package leetcode;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-12-12 10:31
 **/
public class LeetCode5952 {

    //大佬题解
    public int countPoints(String rings) {
        HashMap<Integer, HashSet<Character>> map = new HashMap<>();
        for (int i = 0; i < rings.length(); i += 2) {
            // java8之后。上面的操作可以简化为一行，若key对应的value为空，会将第二个参数的返回值存入并返回
            map.computeIfAbsent(rings.charAt(i + 1) - '0', t -> new HashSet<>()).add(rings.charAt(i));
        }
        int count = 0;
        for (HashSet<Character> set : map.values()) {
            count += set.size() / 3;
        }

        return count;
    }

    /*public int countPoints(String rings) {
        Map<Integer, Set<Character>> map = new HashMap<>();
        int len = rings.length();
        for (int i = 0; i < len; i += 2) {
            char ch1 = rings.charAt(i);
            int number = rings.charAt(i + 1) - '0';
            Set<Character> set = map.get(number);
            if (set == null) {
                set = new HashSet<>();
                map.put(number, set);
            }
            set.add(ch1);

        }

        int count = 0;
        Set<Integer> integers = map.keySet();
        for (Integer integer : integers) {
            if (map.get(integer).size() == 3) {
                count++;
            }
        }
        return count;
    }*/

    public static void main(String[] args) {
        LeetCode5952 code = new LeetCode5952();
        System.out.println("code.countPoints(\"B0B6G0R6R0R6G9\") = " + code.countPoints("B0B6G0R6R0R6G9"));
    }
}
